<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Cola\Mapper;

use App\Cola\Model\ColaBill;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 账本记录Mapper类
 */
class ColaBillMapper extends AbstractMapper
{
    /**
     * @var ColaBill
     */
    public $model;

    public function assignModel()
    {
        $this->model = ColaBill::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 交易来源
        if (isset($params['pay_source']) && filled($params['pay_source'])) {
            $query->where('pay_source', '=', $params['pay_source']);
        }

        // 支付时间
        if (isset($params['pay_time']) && filled($params['pay_time']) && is_array($params['pay_time']) && count($params['pay_time']) == 2) {
            $query->whereBetween(
                'pay_time',
                [ $params['pay_time'][0], $params['pay_time'][1] ]
            );
        }

        // 交易分类
        if (isset($params['trade_cate_id']) && filled($params['trade_cate_id'])) {
            $query->where('trade_cate_id', '=', $params['trade_cate_id']);
        }

        // 交易对方
        if (isset($params['counterparty']) && filled($params['counterparty'])) {
            $query->where('counterparty', 'like', '%'.$params['counterparty'].'%');
        }

        // 交易对方账号
        if (isset($params['counterparty_account']) && filled($params['counterparty_account'])) {
            $query->where('counterparty_account', 'like', '%'.$params['counterparty_account'].'%');
        }

        // 商品说明
        if (isset($params['goods']) && filled($params['goods'])) {
            $query->where('goods', 'like', '%'.$params['goods'].'%');
        }

        // 收支类型
        if (isset($params['cash_flow']) && filled($params['cash_flow'])) {
            $query->where('cash_flow', '=', $params['cash_flow']);
        }

        // 交易金额
        if (isset($params['money']) && filled($params['money'])) {
            $query->where('money', '=', $params['money']);
        }

        // 收/付款方式
        if (isset($params['payment_method']) && filled($params['payment_method'])) {
            $query->where('payment_method', 'like', '%'.$params['payment_method'].'%');
        }

        // 交易状态
        if (isset($params['trade_status']) && filled($params['trade_status'])) {
            $query->where('trade_status', 'like', '%'.$params['trade_status'].'%');
        }

        // 交易单号
        if (isset($params['trade_sn']) && filled($params['trade_sn'])) {
            $query->where('trade_sn', 'like', '%'.$params['trade_sn'].'%');
        }

        // 交易商户单号
        if (isset($params['trade_store_sn']) && filled($params['trade_store_sn'])) {
            $query->where('trade_store_sn', 'like', '%'.$params['trade_store_sn'].'%');
        }

        // 备注
        if (isset($params['remark']) && filled($params['remark'])) {
            $query->where('remark', 'like', '%'.$params['remark'].'%');
        }

        // 创建时间
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 修改时间
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 删除时间
        if (isset($params['deleted_at']) && filled($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }

}